package org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.udp.advanced;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.netty.permeate.event.ClientChangeEvent;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.udp.client.AbstractHandleUdpDistributeConnectSuccessNotificationTypeAdvancedHandle;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;

import java.util.List;

/**
 * 客户端连接成功通知
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
public class ClientHandleUdpDistributeConnectSuccessNotificationTypeAdvancedHandle extends AbstractHandleUdpDistributeConnectSuccessNotificationTypeAdvancedHandle<NettyProxyMsg> {

    private final ClientChangeEvent clientChangeEvent;


    public ClientHandleUdpDistributeConnectSuccessNotificationTypeAdvancedHandle(ClientChangeEvent clientChangeEvent) {
        this.clientChangeEvent = clientChangeEvent;
    }

    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param msg     通道数据
     */
    @Override
    protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg msg) {
        // 客户端ID：{},客户端:{}连接成功
        log.warn("Client ID: {}, Client Data : {} Connection successful", new String(msg.getClientId()), new String(msg.getData()));


        // 存储其他客户端状态
        List<String> clientIdList = JSONObject.parseArray(new String(msg.getData()), String.class);
        for (String clientId : clientIdList) {
            clientChangeEvent.clientOnLine(clientId);
        }

    }
}
